package com.bowlong.third.redis;

import java.util.Map;

import redis.clients.jedis.JedisPool;

import com.bowlong.util.LRUCache;
import com.bowlong.util.MultiKey2;

// 多层次LRU缓存
@SuppressWarnings("unchecked")
public class MultLRUJedis<V> {
	final JedisPool pool; // redis 连接池
	final Map<MultiKey2, V> inside; // 进程内部

	public MultLRUJedis(JedisPool pool, int insideSize) {
		this.pool = pool;
		this.inside = LRUCache.createSafe(insideSize);
	}

	public void put(MultiKey2 key, V value) throws Exception {
		inside.put(key, value);

		String sk = key.toString();
		JedisEx.putObject(pool, sk, value);
	}

	public V get(MultiKey2 key) throws Exception {
		V v = inside.get(key);
		if (v != null)
			return v;

		String sk = key.toString();
		v = (V) JedisEx.getObject(sk);
		return v;
	}

	public void remove(MultiKey2 key) {
		inside.remove(key);

		String sk = key.toString();
		JedisEx.remove(sk);
	}
}
